<template>
    <div class="login_container">
        <div class="login_box">
            <div class="avatar_box">
                <img src="../assets/logo.png"/>
            </div>
            <el-form class="login_form" ref="loginFormRef" :model="form" :rules="rules">
                <el-form-item prop="username">
                    <el-input prefix-icon="iconfont icon-user" v-model="form.username"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-input prefix-icon="iconfont icon-3702mima" v-model="form.password" type="password"></el-input>
                </el-form-item>
                <el-form-item class="btn">
                    <el-button type="primary" @click="onSubmit">登录</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>    
</template>

<script>
export default {
    data() {
      return {
        form: {
          username: 'admin',
          password:'admin'
        },
        rules: {
          username: [
            { required: true, message: '请输入用户名', trigger: 'blur' },
            { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
          ],
          pas: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
          ]
        }
      } 
    },
    methods: {
      onSubmit() {
        this.$refs.loginFormRef.validate(async valid=>{
          if(!valid)return;
          //测试情况下，用get请求
          const {data:res}= await this.$http.get('/json/login.json',this.form);
          //保存token
          window.sessionStorage.setItem('token',res.data.token);
          if(res.meta.status!=200) return this.$message.success('登录失败');
          this.$message.success('登录成功');
          //跳转
          this.$router.push('/home');
        })
      }
    }
}
</script>

<style lang="less" scoped>
.login_container{
    background-color: #2b4b6b;
    height: 100%
};
.login_box{
    width: 45vw;
    height: 30vw;
    background-color: white;
    border-radius: 1vw;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);

    .avatar_box{
        height: 10vw;
        width: 10vw;
        border: 1vw solid #eee;
        border-radius: 50%;
        padding: 1vw;
        box-shadow: 0 0 10px #ddd;
        position: absolute;
        left: 50%;
        transform: translate(-50%,-50%);
        background-color: #fff;
        img{
            width: 100%;
            height: 100%;
            border-radius: 50%;
            background-color: #eee;
        }
    }
}

.login_form{
    position: absolute;
    bottom: 3vw;
    width: 100%;
    padding: 0 4vw;
    box-sizing: border-box;
}

.btn{
    display: flex;
    justify-content: center;
    padding: 2vw 0 0 0;
}
</style>

